package dcc.tools;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import dcc.app.ManagerGroup;
import dcc.component.DataExportTable;
import dcc.component.JTaskPaneX;
import dcc.mbg.model.User;
import dcc.service.DataExportService;
import dcc.ui.SearchWindow;

public class Tools {

	private Properties props;
	private SearchWindow searchWindow;
	private JTaskPaneX taskPane;
	private ManagerGroup managerGroup;

	private Map<String, String> talentTypeMapToSymbol;
	private Map<String, String> talentTypeMapToLabel;

	private User user;

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return this.user;
	}

	public String getTalentTypeSymbol(String key) {
		return this.talentTypeMapToSymbol.get(key);
	}

	public Map<String, String> getTalentTypeMapToLabel() {
		return this.talentTypeMapToLabel;
	}

	public JTaskPaneX getTaskPane() {
		return taskPane;
	}

	private static Tools tools;

	private Tools() {

	}

	public static Tools get() {
		if (Tools.tools == null) {
			Tools.tools = new Tools();
		}
		return Tools.tools;
	}

	public void addSearchWindowComponent(SearchWindow searchWindow) {
		this.searchWindow = searchWindow;
	}

	public void addTaskPaneComponent(JTaskPaneX taskPane) {
		this.taskPane = taskPane;
	}

	public void loadProperties(String filePath) throws Exception {
		InputStream is = null;
		try {
			props = new Properties();
			is = getClass().getResourceAsStream(filePath);
			props.load(is);
			talentTypeMapToSymbol = new HashMap<String, String>();
			talentTypeMapToLabel = new HashMap<String, String>();
			Iterator<Object> iterator = props.keySet().iterator();
			String key = null;
			while (iterator.hasNext()) {
				key = iterator.next().toString();
				if (key.toString().startsWith("TYPE_")) {
					talentTypeMapToLabel.put(key.substring(5),
							props.getProperty(key));
					talentTypeMapToSymbol.put(props.getProperty(key),
							key.substring(5));
				}
			}
			dataExportTables = new ArrayList<DataExportTable>();
			dataExportService = new DataExportService();
		} catch (Exception e) {
			throw e;
		} finally {
			if (is != null) {
				is.close();
			}
		}
	}

	public String getValue(String key) {
		return "" + props.get(key);
	}

	public Set<Object> keySet() {
		return props.keySet();
	}

	public SearchWindow getSearchWindow() {
		return searchWindow;
	}

	public void addManagerGroup(ManagerGroup managerGroup) {
		this.managerGroup = managerGroup;
	}

	public ManagerGroup getManagerGroup() {
		return this.managerGroup;
	}

	private List<DataExportTable> dataExportTables;

	public void addDataExportTable(DataExportTable dataExportTable) {
		this.dataExportTables.add(dataExportTable);
	}

	public List<DataExportTable> getDataExportTables() {
		return dataExportTables;
	}

	private DataExportService dataExportService;

	public DataExportService getDataExportService() {
		return dataExportService;
	}
}
